Walkthrough 11-5: Define and use variables and functions

In this walkthrough, you continue to work with the DataWeave transformation in postMultipleFlights. You will:

·       Define and use a global constant.

·       Define and use a global variable that is equal to a lambda expression.

·       Define and use a lambda expression assigned to a variable as a function.

·       Define and use a local variable.

 

Starting file

If you did not complete the previous walkthrough, you can get a starting file here. This file is also located in the solutions folder of the student files ZIP located in the Course Resources.

Define and use a global constant

1.     Return to the Transform Message properties view for the transformation in postMultipleFlights.

2.     Change the output type to application/dw.

3.     In the header, define a global variable called numSeats that is equal to 400.

var numSeats = 400

4.     In the body, add a totalSeats property that is equal to the numSeats constant.

totalSeats: numSeats

5.     Look at the preview.

6.     Comment out the variable declaration in the header.

//var numSeats = 400

Define and use a global variable that is equal to a lambda expression that maps to a constant

7.     In the header, define a global variable called numSeats that is equal to a lambda expression with an input parameter x equal to 400.

8.     Inside the expression, set numSeats to x.

var numSeats = (x=400) -> x

9.     Add parentheses after numSeats in the totalSeats property assignment.

totalSeats: numSeats()

10.  Look at the preview.

Add a plane property to the output

11.  Add a plane property to the DataWeave expression equal to the value of the input planeType values.

12.  Look at the values of plane type in the preview.

Define and use a global variable that is that is equal to a lambda expression with input parameters

13.  Comment out the numSeats variable declaration in the header.

14.  In the header, define a global variable called numSeats that is equal to a lambda expression with an input parameter called planeType of type String.

var numSeats = (planeType: String) ->

15.  Inside the expression, add an if/else block that checks to see if planeType contains the string 737 and sets numSeats to 150 or 300.

16.  Change the totalSeats property assignment to pass the object’s planeType property to numSeats.

totalSeats: numSeats(object.planeType)

17.  Force the argument to a String.

totalSeats: numSeats(object.planeType as String)

18.  Look at the preview; you should see values of 150 and 300.

19.  Surround the variable declaration in the header with /* and */ to comment it out.

Define and use a lambda expression assigned to a variable as a function

20.  In the header, use the fun keyword to define a function called getNumSeats with a parameter called planeType of type String.

fun getNumSeats(planeType: String)

21.  Copy the if/else expression in the numSeats declaration.

22.  Set the getNumSeats function equal to the if/else expression.

fun getNumSeats(planeType: String) =

     if (planeType contains('737'))

          150

     else

          300

23.  In the body, change the totalSeats property to be equal to the result of the getNumSeats function.

totalSeats: getNumSeats(object.planeType as String)

24.  Look at the preview.

 

Create and use a local variable

25.  In the body, surround the existing DataWeave expression with parentheses and add the using keyword in front of it.

 

26.  Change the colon after flights to an equal sign.

27.  Modify the indentation to your liking.

28.  After the local variable declaration, set the transformation expression to be the local flights variable.

29.  Look at the preview.

 



Did you complete the walkthrough?

  Yes, I completed the walkthrough

  No, I did not complete the walkthrough

  I completed part of the walkthrough


Comments and/or feedback